package cn.hznc.controller.service.toolAnalyze;

import cn.hutool.core.collection.CollectionUtil;
import cn.hznc.domain.entity.*;
import cn.hznc.domain.request.toolAnalyze.IntelligentToolChartReq;
import cn.hznc.mapper.ICabinetRecordMapper;
import cn.hznc.mapper.TypeMapper;
import cn.hznc.vo.IntelligentToolChartVo;
import cn.hznc.vo.ToolTypeAnalysisSimpleVo;
import cn.hznc.vo.ToolTypeAnalysisVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class IntelligentToolChartService {
    @Resource
    private ICabinetRecordMapper iCabinetRecordMapper;
    @Resource
    private TypeMapper typeMapper;

    @Transactional
    public List<IntelligentToolChartVo> queryIntelligentToolChartByDay(IntelligentToolChartReq intelligentToolChartReq){
        List<Date> dates = new ArrayList();
        dates.add(intelligentToolChartReq.getStartTime());
        Calendar calBegin = Calendar.getInstance();

        calBegin.setTime(intelligentToolChartReq.getStartTime());
        while (intelligentToolChartReq.getEndTime().after(calBegin.getTime())) {
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            dates.add(calBegin.getTime());
        }
        if (CollectionUtil.isEmpty(intelligentToolChartReq.getIds())){
            List<Integer> ids = new ArrayList<>();
            LambdaQueryWrapper<TypeEntity> typeEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            typeEntityLambdaQueryWrapper.eq(TypeEntity::getParentId,0);
            List<TypeEntity> typeEntities = typeMapper.selectList(typeEntityLambdaQueryWrapper);
            typeEntities.forEach(typeEntity -> {
                ids.add(typeEntity.getId());
            });
            intelligentToolChartReq.setIds(ids);
        }
        List<IntelligentToolChartEntity> intelligentToolChartEntities = new ArrayList<>();
        intelligentToolChartReq.getIds().forEach(integer -> {
            List<String> partnos = new ArrayList<>();
            LambdaQueryWrapper<TypeEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(TypeEntity::getParentId,integer);
            List<TypeEntity> typeEntities = typeMapper.selectList(lambdaQueryWrapper);
            if (CollectionUtil.isNotEmpty(typeEntities)){
                typeEntities.forEach(typeEntity -> {
                    partnos.add(typeEntity.getTypeName());
                });
            }
            TypeEntity typeEntity = typeMapper.selectById(integer);
            LambdaQueryWrapper<ICabinetRecordEntity> queryWrapper = new LambdaQueryWrapper<>();
            if (CollectionUtil.isNotEmpty(partnos)){
                queryWrapper.in(ICabinetRecordEntity::getPartno,partnos);
            }else {
                queryWrapper.eq(ICabinetRecordEntity::getPartno,"不存在");
            }
            if (intelligentToolChartReq.getType().equals("inbound")){
                queryWrapper.eq(ICabinetRecordEntity::getType,"加料单");
            }else if (intelligentToolChartReq.getType().equals("outbound")){
                queryWrapper.eq(ICabinetRecordEntity::getType,"取料单");
            }
            if (intelligentToolChartReq.getStartTime() != null&&intelligentToolChartReq.getEndTime()!= null){
//                if (intelligentToolChartReq.getStartTime().equals(intelligentToolChartReq.getEndTime())){
                    Date date = intelligentToolChartReq.getEndTime();
                    Calendar calendar = new GregorianCalendar();
                    calendar.setTime(date);
                    calendar.add(Calendar.DATE, 1);
                    date = calendar.getTime();
                    intelligentToolChartReq.setEndTime(date);
//                }
                queryWrapper.between(ICabinetRecordEntity::getTime,intelligentToolChartReq.getStartTime(),intelligentToolChartReq.getEndTime());
            }else {
                queryWrapper.between(ICabinetRecordEntity::getTime, LocalDate.now().minusWeeks(1),LocalDate.now());
            }
            queryWrapper.select(ICabinetRecordEntity::getTime,ICabinetRecordEntity::getNumber);
            List<ICabinetRecordEntity> iCabinetRecordEntityList = iCabinetRecordMapper.selectList(queryWrapper);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            iCabinetRecordEntityList.forEach(iCabinetRecordEntity -> {
                String s = simpleDateFormat.format(iCabinetRecordEntity.getTime());
                try {
                    iCabinetRecordEntity.setTime(simpleDateFormat.parse(s));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            });

            Map<Date,List<ICabinetRecordEntity>> map = iCabinetRecordEntityList.stream().collect(Collectors.groupingBy(ICabinetRecordEntity::getTime));
            Iterator<Map.Entry<Date, List<ICabinetRecordEntity>>> entries = map.entrySet().iterator();

            while (entries.hasNext()){
                Map.Entry<Date, List<ICabinetRecordEntity>> entry = entries.next();
                IntelligentToolChartEntity intelligentToolChartEntity = new IntelligentToolChartEntity();
                List<Integer> integers = new ArrayList<>();
                entry.getValue().forEach(iCabinetRecordEntity -> {
                    if (StringUtil.isNotEmpty(iCabinetRecordEntity.getNumber())){
                        integers.add(Integer.parseInt(iCabinetRecordEntity.getNumber()));
                    }
                });
                if (CollectionUtil.isNotEmpty(integers)){
                    intelligentToolChartEntity.setDate(simpleDateFormat.format(entry.getKey()));
                    intelligentToolChartEntity.setTypeName(typeEntity.getTypeName());
                    intelligentToolChartEntity.setNumber(integers.stream().reduce(Integer::sum).orElse(0));
//                        menuIdAndMenuNameEntity.setLabel(typeEntity.getTypeName());
//                        menuIdAndMenuNameEntity.setValue(integers.stream().reduce(Integer::sum).orElse(0));
                }else {
                    intelligentToolChartEntity.setDate(simpleDateFormat.format(entry.getKey()));
                    intelligentToolChartEntity.setTypeName(typeEntity.getTypeName());
                    intelligentToolChartEntity.setNumber(0);
//                        menuIdAndMenuNameEntity.setLabel(typeEntity.getTypeName());
//                        menuIdAndMenuNameEntity.setValue(0);
                }
//                menuIdAndMenuNameEntityList.add(menuIdAndMenuNameEntity);
                intelligentToolChartEntities.add(intelligentToolChartEntity);
//                dates.remove(entry.getKey());
            }
            for (Date date: dates){
                IntelligentToolChartEntity intelligentToolChartEntity = new IntelligentToolChartEntity();
                intelligentToolChartEntity.setDate(simpleDateFormat.format(date));
                intelligentToolChartEntity.setTypeName(typeEntity.getTypeName());
                intelligentToolChartEntity.setNumber(0);
                List<Integer> integers = new ArrayList<>();
                intelligentToolChartEntities.forEach(intelligentToolChartEntity1 -> {
                    if (intelligentToolChartEntity1.getTypeName().equals(intelligentToolChartEntity.getTypeName())&&
                    intelligentToolChartEntity1.getDate().equals(intelligentToolChartEntity.getDate())){
                        integers.add(1);
                    }
                });
//                    MenuIdAndMenuNameEntity menuIdAndMenuNameEntity = new MenuIdAndMenuNameEntity();
//                    menuIdAndMenuNameEntity.setLabel(simpleDateFormat.format(date));
//                    menuIdAndMenuNameEntity.setValue(0);
//                    menuIdAndMenuNameEntityList.add(menuIdAndMenuNameEntity);
                if (CollectionUtil.isEmpty(integers))
                intelligentToolChartEntities.add(intelligentToolChartEntity);

            }
        });
        List<IntelligentToolChartVo> toolTypeAnalysisVos = new ArrayList<>();
        Map<String,List<IntelligentToolChartEntity>> map = intelligentToolChartEntities.stream().collect(Collectors.groupingBy(IntelligentToolChartEntity::getDate));
        Iterator<Map.Entry<String, List<IntelligentToolChartEntity>>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, List<IntelligentToolChartEntity>> entry = entries.next();
            IntelligentToolChartVo intelligentToolChartVo = new IntelligentToolChartVo();
            intelligentToolChartVo.setDate(entry.getKey());
            intelligentToolChartVo.setList(entry.getValue());
            toolTypeAnalysisVos.add(intelligentToolChartVo);
        }
        List<IntelligentToolChartVo> toolTypeAnalysisVos1 =  toolTypeAnalysisVos.stream().sorted(Comparator.comparing(IntelligentToolChartVo::getDate)).collect(Collectors.toList());

        return toolTypeAnalysisVos1;
    }
    @Transactional
    public List<IntelligentToolChartVo> queryIntelligentToolChartByMonth(IntelligentToolChartReq intelligentToolChartReq){
        List<Date> dates = new ArrayList();
        dates.add(intelligentToolChartReq.getStartTime());
        Calendar calBegin = Calendar.getInstance();

        calBegin.setTime(intelligentToolChartReq.getStartTime());
        while (intelligentToolChartReq.getEndTime().after(calBegin.getTime())) {
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            dates.add(calBegin.getTime());
        }
        if (CollectionUtil.isEmpty(intelligentToolChartReq.getIds())){
            List<Integer> ids = new ArrayList<>();
            LambdaQueryWrapper<TypeEntity> typeEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            typeEntityLambdaQueryWrapper.eq(TypeEntity::getParentId,0);
            List<TypeEntity> typeEntities = typeMapper.selectList(typeEntityLambdaQueryWrapper);
            typeEntities.forEach(typeEntity -> {
                ids.add(typeEntity.getId());
            });
            intelligentToolChartReq.setIds(ids);
        }
        List<IntelligentToolChartEntity> intelligentToolChartEntities = new ArrayList<>();
        intelligentToolChartReq.getIds().forEach(integer -> {
            List<String> partnos = new ArrayList<>();
            LambdaQueryWrapper<TypeEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(TypeEntity::getParentId,integer);
            List<TypeEntity> typeEntities = typeMapper.selectList(lambdaQueryWrapper);
            if (CollectionUtil.isNotEmpty(typeEntities)){
                typeEntities.forEach(typeEntity -> {
                    partnos.add(typeEntity.getTypeName());
                });
            }
            TypeEntity typeEntity = typeMapper.selectById(integer);
            LambdaQueryWrapper<ICabinetRecordEntity> queryWrapper = new LambdaQueryWrapper<>();
            if (CollectionUtil.isNotEmpty(partnos)){
                queryWrapper.in(ICabinetRecordEntity::getPartno,partnos);
            }else {
                queryWrapper.eq(ICabinetRecordEntity::getPartno,"不存在");
            }
            if (intelligentToolChartReq.getType().equals("inbound")){
                queryWrapper.eq(ICabinetRecordEntity::getType,"加料单");
            }else if (intelligentToolChartReq.getType().equals("outbound")){
                queryWrapper.eq(ICabinetRecordEntity::getType,"取料单");
            }
            if (intelligentToolChartReq.getStartTime() != null&&intelligentToolChartReq.getEndTime()!= null){
//                if (intelligentToolChartReq.getStartTime().equals(intelligentToolChartReq.getEndTime())){
                Date date = intelligentToolChartReq.getEndTime();
                Calendar calendar = new GregorianCalendar();
                calendar.setTime(date);
                calendar.add(Calendar.MONTH, 1);
                date = calendar.getTime();
                intelligentToolChartReq.setEndTime(date);
//                }
                queryWrapper.between(ICabinetRecordEntity::getTime,intelligentToolChartReq.getStartTime(),intelligentToolChartReq.getEndTime());
            }else {
                queryWrapper.between(ICabinetRecordEntity::getTime, LocalDate.now().minusWeeks(1),LocalDate.now());
            }
            queryWrapper.select(ICabinetRecordEntity::getTime,ICabinetRecordEntity::getNumber);
            List<ICabinetRecordEntity> iCabinetRecordEntityList = iCabinetRecordMapper.selectList(queryWrapper);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            iCabinetRecordEntityList.forEach(iCabinetRecordEntity -> {
                String s = simpleDateFormat.format(iCabinetRecordEntity.getTime());
                try {
                    iCabinetRecordEntity.setTime(simpleDateFormat.parse(s));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            });

            Map<Date,List<ICabinetRecordEntity>> map = iCabinetRecordEntityList.stream().collect(Collectors.groupingBy(ICabinetRecordEntity::getTime));
            Iterator<Map.Entry<Date, List<ICabinetRecordEntity>>> entries = map.entrySet().iterator();

            while (entries.hasNext()){
                Map.Entry<Date, List<ICabinetRecordEntity>> entry = entries.next();
                IntelligentToolChartEntity intelligentToolChartEntity = new IntelligentToolChartEntity();
                List<Integer> integers = new ArrayList<>();
                entry.getValue().forEach(iCabinetRecordEntity -> {
                    if (StringUtil.isNotEmpty(iCabinetRecordEntity.getNumber())){
                        integers.add(Integer.parseInt(iCabinetRecordEntity.getNumber()));
                    }
                });
                if (CollectionUtil.isNotEmpty(integers)){
                    intelligentToolChartEntity.setDate(simpleDateFormat.format(entry.getKey()));
                    intelligentToolChartEntity.setTypeName(typeEntity.getTypeName());
                    intelligentToolChartEntity.setNumber(integers.stream().reduce(Integer::sum).orElse(0));
//                        menuIdAndMenuNameEntity.setLabel(typeEntity.getTypeName());
//                        menuIdAndMenuNameEntity.setValue(integers.stream().reduce(Integer::sum).orElse(0));
                }else {
                    intelligentToolChartEntity.setDate(simpleDateFormat.format(entry.getKey()));
                    intelligentToolChartEntity.setTypeName(typeEntity.getTypeName());
                    intelligentToolChartEntity.setNumber(0);
//                        menuIdAndMenuNameEntity.setLabel(typeEntity.getTypeName());
//                        menuIdAndMenuNameEntity.setValue(0);
                }
//                menuIdAndMenuNameEntityList.add(menuIdAndMenuNameEntity);
                intelligentToolChartEntities.add(intelligentToolChartEntity);
//                dates.remove(entry.getKey());
            }
            for (Date date: dates){
                IntelligentToolChartEntity intelligentToolChartEntity = new IntelligentToolChartEntity();
                intelligentToolChartEntity.setDate(simpleDateFormat.format(date));
                intelligentToolChartEntity.setTypeName(typeEntity.getTypeName());
                intelligentToolChartEntity.setNumber(0);
                List<Integer> integers = new ArrayList<>();
                intelligentToolChartEntities.forEach(intelligentToolChartEntity1 -> {
                    if (intelligentToolChartEntity1.getTypeName().equals(intelligentToolChartEntity.getTypeName())&&
                            intelligentToolChartEntity1.getDate().equals(intelligentToolChartEntity.getDate())){
                        integers.add(1);
                    }
                });
//                    MenuIdAndMenuNameEntity menuIdAndMenuNameEntity = new MenuIdAndMenuNameEntity();
//                    menuIdAndMenuNameEntity.setLabel(simpleDateFormat.format(date));
//                    menuIdAndMenuNameEntity.setValue(0);
//                    menuIdAndMenuNameEntityList.add(menuIdAndMenuNameEntity);
                if (CollectionUtil.isEmpty(integers))
                    intelligentToolChartEntities.add(intelligentToolChartEntity);

            }
        });
        List<IntelligentToolChartVo> toolTypeAnalysisVos = new ArrayList<>();
        Map<String,List<IntelligentToolChartEntity>> map = intelligentToolChartEntities.stream().collect(Collectors.groupingBy(IntelligentToolChartEntity::getDate));
        Iterator<Map.Entry<String, List<IntelligentToolChartEntity>>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, List<IntelligentToolChartEntity>> entry = entries.next();
            IntelligentToolChartVo intelligentToolChartVo = new IntelligentToolChartVo();
            intelligentToolChartVo.setDate(entry.getKey());
            intelligentToolChartVo.setList(entry.getValue());
            toolTypeAnalysisVos.add(intelligentToolChartVo);
        }

        List<IntelligentToolChartVo> toolTypeAnalysisVos1 =  toolTypeAnalysisVos.stream().sorted(Comparator.comparing(IntelligentToolChartVo::getDate)).collect(Collectors.toList());
        return toolTypeAnalysisVos1;

    }

    @Transactional
    public List<Map<String,Object>> queryIntelligentToolRecordByMonth(IntelligentToolChartReq intelligentToolChartReq){
        List<Date> dates = new ArrayList();
        dates.add(intelligentToolChartReq.getStartTime());
        Calendar calBegin = Calendar.getInstance();

        calBegin.setTime(intelligentToolChartReq.getStartTime());
        while (intelligentToolChartReq.getEndTime().after(calBegin.getTime())) {
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
            dates.add(calBegin.getTime());
        }
        if (CollectionUtil.isEmpty(intelligentToolChartReq.getIds())){
            List<Integer> ids = new ArrayList<>();
            LambdaQueryWrapper<TypeEntity> typeEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
            typeEntityLambdaQueryWrapper.eq(TypeEntity::getParentId,0);
            List<TypeEntity> typeEntities = typeMapper.selectList(typeEntityLambdaQueryWrapper);
            typeEntities.forEach(typeEntity -> {
                ids.add(typeEntity.getId());
            });
            intelligentToolChartReq.setIds(ids);
        }
        List<IntelligentToolChartEntity> intelligentToolChartEntities = new ArrayList<>();
        intelligentToolChartReq.getIds().forEach(integer -> {
            List<String> partnos = new ArrayList<>();
            LambdaQueryWrapper<TypeEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(TypeEntity::getParentId,integer);
            List<TypeEntity> typeEntities = typeMapper.selectList(lambdaQueryWrapper);
            if (CollectionUtil.isNotEmpty(typeEntities)){
                typeEntities.forEach(typeEntity -> {
                    partnos.add(typeEntity.getTypeName());
                });
            }
            TypeEntity typeEntity = typeMapper.selectById(integer);
            LambdaQueryWrapper<ICabinetRecordEntity> queryWrapper = new LambdaQueryWrapper<>();
            if (CollectionUtil.isNotEmpty(partnos)){
                queryWrapper.in(ICabinetRecordEntity::getPartno,partnos);
            }else {
                queryWrapper.eq(ICabinetRecordEntity::getPartno,"不存在");
            }
            if (intelligentToolChartReq.getType().equals("inbound")){
                queryWrapper.eq(ICabinetRecordEntity::getType,"加料单");
            }else if (intelligentToolChartReq.getType().equals("outbound")){
                queryWrapper.eq(ICabinetRecordEntity::getType,"取料单");
            }
            if (intelligentToolChartReq.getStartTime() != null&&intelligentToolChartReq.getEndTime()!= null){
//                if (intelligentToolChartReq.getStartTime().equals(intelligentToolChartReq.getEndTime())){
                Date date = intelligentToolChartReq.getEndTime();
                Calendar calendar = new GregorianCalendar();
                calendar.setTime(date);
                calendar.add(Calendar.MONTH, 1);
                date = calendar.getTime();
                intelligentToolChartReq.setEndTime(date);
//                }
                queryWrapper.between(ICabinetRecordEntity::getTime,intelligentToolChartReq.getStartTime(),intelligentToolChartReq.getEndTime());
            }else {
                queryWrapper.between(ICabinetRecordEntity::getTime, LocalDate.now().minusWeeks(1),LocalDate.now());
            }
            queryWrapper.select(ICabinetRecordEntity::getTime,ICabinetRecordEntity::getNumber);
            List<ICabinetRecordEntity> iCabinetRecordEntityList = iCabinetRecordMapper.selectList(queryWrapper);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM");
            iCabinetRecordEntityList.forEach(iCabinetRecordEntity -> {
                String s = simpleDateFormat.format(iCabinetRecordEntity.getTime());
                try {
                    iCabinetRecordEntity.setTime(simpleDateFormat.parse(s));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            });

            Map<Date,List<ICabinetRecordEntity>> map = iCabinetRecordEntityList.stream().collect(Collectors.groupingBy(ICabinetRecordEntity::getTime));
            Iterator<Map.Entry<Date, List<ICabinetRecordEntity>>> entries = map.entrySet().iterator();

            while (entries.hasNext()){
                Map.Entry<Date, List<ICabinetRecordEntity>> entry = entries.next();
                IntelligentToolChartEntity intelligentToolChartEntity = new IntelligentToolChartEntity();
                List<Integer> integers = new ArrayList<>();
                entry.getValue().forEach(iCabinetRecordEntity -> {
                    if (StringUtil.isNotEmpty(iCabinetRecordEntity.getNumber())){
                        integers.add(Integer.parseInt(iCabinetRecordEntity.getNumber()));
                    }
                });
                if (CollectionUtil.isNotEmpty(integers)){
                    intelligentToolChartEntity.setDate(simpleDateFormat.format(entry.getKey()));
                    intelligentToolChartEntity.setTypeName(typeEntity.getTypeName());
                    intelligentToolChartEntity.setNumber(integers.stream().reduce(Integer::sum).orElse(0));
//                        menuIdAndMenuNameEntity.setLabel(typeEntity.getTypeName());
//                        menuIdAndMenuNameEntity.setValue(integers.stream().reduce(Integer::sum).orElse(0));
                }else {
                    intelligentToolChartEntity.setDate(simpleDateFormat.format(entry.getKey()));
                    intelligentToolChartEntity.setTypeName(typeEntity.getTypeName());
                    intelligentToolChartEntity.setNumber(0);
//                        menuIdAndMenuNameEntity.setLabel(typeEntity.getTypeName());
//                        menuIdAndMenuNameEntity.setValue(0);
                }
//                menuIdAndMenuNameEntityList.add(menuIdAndMenuNameEntity);
                intelligentToolChartEntities.add(intelligentToolChartEntity);
//                dates.remove(entry.getKey());
            }
            for (Date date: dates){
                IntelligentToolChartEntity intelligentToolChartEntity = new IntelligentToolChartEntity();
                intelligentToolChartEntity.setDate(simpleDateFormat.format(date));
                intelligentToolChartEntity.setTypeName(typeEntity.getTypeName());
                intelligentToolChartEntity.setNumber(0);
                List<Integer> integers = new ArrayList<>();
                intelligentToolChartEntities.forEach(intelligentToolChartEntity1 -> {
                    if (intelligentToolChartEntity1.getTypeName().equals(intelligentToolChartEntity.getTypeName())&&
                            intelligentToolChartEntity1.getDate().equals(intelligentToolChartEntity.getDate())){
                        integers.add(1);
                    }
                });
//                    MenuIdAndMenuNameEntity menuIdAndMenuNameEntity = new MenuIdAndMenuNameEntity();
//                    menuIdAndMenuNameEntity.setLabel(simpleDateFormat.format(date));
//                    menuIdAndMenuNameEntity.setValue(0);
//                    menuIdAndMenuNameEntityList.add(menuIdAndMenuNameEntity);
                if (CollectionUtil.isEmpty(integers))
                    intelligentToolChartEntities.add(intelligentToolChartEntity);

            }
        });
        List<Map<String,Object>> mapList = new ArrayList<>();
//        List<IntelligentToolChartVo> toolTypeAnalysisVos = new ArrayList<>();
        List<IntelligentToolChartEntity> intelligentToolChartEntities1 = intelligentToolChartEntities.stream().sorted(Comparator.comparing(IntelligentToolChartEntity::getDate)).collect(Collectors.toList());
        Map<String,List<IntelligentToolChartEntity>> map = intelligentToolChartEntities1.stream().collect(Collectors.groupingBy(IntelligentToolChartEntity::getTypeName));
        Iterator<Map.Entry<String, List<IntelligentToolChartEntity>>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map<String,Object> map1 = new LinkedHashMap<>();
            Map.Entry<String, List<IntelligentToolChartEntity>> entry = entries.next();
            IntelligentToolChartVo intelligentToolChartVo = new IntelligentToolChartVo();
            intelligentToolChartVo.setDate(entry.getKey());
            intelligentToolChartVo.setList(entry.getValue());
            map1.put("type",entry.getKey());
//            List<Integer> integers = new ArrayList<>();
            entry.getValue().forEach(intelligentToolChartEntity -> {
//                integers.add(intelligentToolChartEntity.getNumber());
                map1.put(intelligentToolChartEntity.getDate(),intelligentToolChartEntity.getNumber());
            });
//            long sum = integers.stream().reduce(Integer::sum).orElse(0);
            mapList.add(map1);
        }
        Map<String,List<IntelligentToolChartEntity>> map2 = intelligentToolChartEntities1.stream().collect(Collectors.groupingBy(IntelligentToolChartEntity::getDate));
        Iterator<Map.Entry<String, List<IntelligentToolChartEntity>>> entries1 = map2.entrySet().iterator();
        Map<String,Object> map1 = new LinkedHashMap<>();
        while (entries1.hasNext()) {
            map1.put("type","合计");
            Map.Entry<String, List<IntelligentToolChartEntity>> entry = entries1.next();
            long sum = entry.getValue().stream().mapToLong(IntelligentToolChartEntity::getNumber).sum();
            map1.put(entry.getKey(),sum);
        }
        mapList.add(map1);
//        List<IntelligentToolChartVo> toolTypeAnalysisVos1 =  toolTypeAnalysisVos.stream().sorted(Comparator.comparing(IntelligentToolChartVo::getDate)).collect(Collectors.toList());
        return mapList;

    }
}
